home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_342 / softfont / convert.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  12KB  |  371 lines

  1. /*************************************************************** 
  2.  *  convert.c     portrait font to landscape  HP LaserJet II format 
  3.  *    Compiled under Lattice C Compiler vers 5.04 
  4.  *
  5.  * Call: convert(portrait[file],landscape[file])
  6.  *
  7.  *  by Tom Lynch, 
  8.  *  version 4 April 90
  9.  ***************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #include <exec/types.h>
  16. #include <libraries/dos.h>
  17.  
  18. #define MAX 200    /* byte X byte array for individual character image */
  19.  
  20. extern long DOSBase;
  21.  
  22. FILE *fp,*fl;
  23.  
  24. int filepos = 0; /* position in file being read  (32 bits in AMIGA */
  25. int filend = 0; /* file end flag  */
  26. unsigned int n = 0;    /* value counter */
  27.  
  28. unsigned char c;        /* character read from portrait font file */
  29.  
  30. unsigned char rdchar();     /* define function to return char */
  31. unsigned char skip();         /* define function to return char */
  32. unsigned char change();     /* define function to return char */
  33. unsigned int measureto();
  34. unsigned int _measureto();
  35. char bitmap();          /* read, rotate, and measure length of image in bytes */
  36. unsigned char base2[10][1];            /* temporary storage for binary string */
  37. unsigned char _read();
  38.  
  39. unsigned char far map[MAX][MAX][1];    /* 'far' is REQUIRED for large arrays */
  40.  
  41. convert(p,l)    /* CLI format: PORTOLAN font.port(old) font.land(new) */
  42. char *p;
  43. char *l;
  44. {
  45.     extern unsigned char c;        /* reference externals */
  46.     extern FILE *fp, *fl; 
  47.     extern unsigned int n;
  48.     extern unsigned char rdchar();
  49.     extern unsigned char skip();
  50.     extern unsigned char change();
  51.     extern char bitmap();
  52.     extern unsigned int measureto();
  53.     extern unsigned int _measureto();    
  54.     extern unsigned char _read();
  55.     extern unsigned char base2[][];
  56.     unsigned char descriptor[20][1];
  57.     unsigned int LeftOffset;
  58.     unsigned int TopOffset;
  59.     unsigned int width, h_dots_p;
  60.     unsigned int height, w_bytes_p;
  61.     int size, cchar, count;
  62.     unsigned int w_dots;     /* landscape width in dots equal to height */
  63.     unsigned int w_bytes;    /* integral number of bytes */
  64.     unsigned int h_dots;    /* landscape height equal to portrait */
  65.     unsigned int h_bytes;
  66.     int a,h,i;
  67.     char *res = "01234567890";
  68.     
  69.     DOSBase = OpenLibrary("dos.library",0);
  70.        
  71.     if ((fp = fopen(p,"r")) == NULL)    {
  72.              return(4); 
  73.          }
  74.            if ((fl = fopen(l,"w")) == NULL)    {
  75.              return(5); 
  76.          }
  77.          
  78. /* files ready, convert portrait file to landscape */
  79.  
  80.     rdchar();      /* look for initial escape character */
  81.     if (c != (char)'\033') {fcloseall(); return(6);}
  82.            skip(2);      /* esc, ')', and 's' of font descriptor */
  83.            measureto ('W');  /* read index 'n' that points to next esc */
  84.            skip(12);      /* skip to orientation byte */
  85.     change(0,1);      /* change from port(0) to land(1) */
  86.     skip(n-13);      /* skip to end of font descriptor */
  87.     rdchar();      /* read first byte (esc) of charater descriptor */
  88.     if (c != (char)'\033') {fcloseall(); return(6);}
  89.     while (filend == 0) {    /* test for end of charaters in font */
  90.         skip (2);    /* '*' and 'c' of char descriptor */
  91.         measureto('E');    /* read charater number */
  92.         cchar = n;    /* current character # */
  93.         rdchar();    /* expecting ecape char */
  94.         if (c != (char)'\033') {fcloseall(); return(6);}
  95.         skip(2);    /* bypass '(' and 's' characters */
  96.         
  97.     /* store char descriptor; must measure length to correct old length 
  98.           then write new descriptor      */
  99.         _measureto('W');    /* read char descriptor length */
  100.         size = n - 16;        /* bitmap size */
  101.         descriptor[0][1] = 'W';
  102.         _read(); descriptor[1][1] = c;    /* Format byte = 4 */
  103.         if (c != (char)'\004') {fcloseall(); return(8);}
  104.         _read(); descriptor[2][1] = c;     /* no continuation byte = 0 */
  105.         _read(); descriptor[3][1] = c;    /* descriptor size = 14 */
  106.         _read(); descriptor[4][1] = c;    /* class, format of data = 1 */
  107.         _read(); descriptor[5][1] = '\001';    /* Orientation 1 = land */
  108.         _read(); descriptor[6][1] = c;    /* reserved by HP = 0 */
  109.  
  110.     /* left offset (portrait) = */        
  111.         _read(); descriptor[7][1] = c;    /* left offset (P) hi byte */
  112.         _read(); descriptor[8][1] = c;    /* left offset (P) lo byte */
  113.  
  114.     /* top offset (port) = */
  115.         _read(); descriptor[9][1] = c;    /* top offset (P) hi byte */
  116.         _read(); descriptor[10][1] = c;    /* top offset (P) lo byte */
  117.  
  118.     /* portrait width is read into landscape height descriptor bits */
  119.         _read(); descriptor[13][1] = c;    /* char width land hi */
  120.         _read(); descriptor[14][1] = c;    /* char width land hi */
  121.         
  122.     /* land */    
  123.         height = 256 * descriptor[13][1] + descriptor[14][1]; /* dots */
  124.         h_dots = height;            /* landscape height pixels */
  125.         h_bytes = height/8; if(height % 8) h_bytes += 1; /* bytes */
  126.         w_bytes_p = h_bytes;
  127.  
  128.     /* portrait height is read into landscape width descriptor bits */
  129.         _read(); descriptor[11][1] = c;    /* char height land */
  130.         _read(); descriptor[12][1] = c;    /* char height land */
  131.  
  132.     /* land */    
  133.         width = 256 * descriptor[11][1] + descriptor[12][1]; /* dots */
  134.         w_dots = width;
  135.         h_dots_p = width;
  136.         w_bytes = width/8; if(width % 8) w_bytes += 1; /* bytes */
  137.  
  138.         count = w_bytes_p * h_dots_p;    /* # of bytes to be read
  139.                            from portrait file */
  140.                            
  141.         _read(); descriptor[15][1] = c;    /* Delta X   hi byte */
  142.         _read(); descriptor[16][1] = c;    /* Delta X   lo byte */
  143.         
  144.     /* top offset (land) = left offset(P) + char width(P) - 1, 16 bit hex */
  145.         TopOffset = 256 * descriptor[7][1] + descriptor[8][1] +        
  146.             256 * descriptor[13][1] + descriptor[14][1] - 1;
  147.             
  148.     /* left offset (landscape) = 0xFFFF - top offset(P) 16 bit hex */
  149.         LeftOffset = 65535 - (256 * descriptor[9][1] + descriptor[10][1]);        
  150.  
  151.         descriptor[7][1] = (unsigned char) (LeftOffset / 256);    /* hi byte */
  152.         descriptor[8][1] = (unsigned char) (LeftOffset % 256);    /* lo byte */
  153.  
  154.         descriptor[9][1] = (unsigned char) TopOffset / 256;    /* hi byte */
  155.         descriptor[10][1] = (unsigned char) TopOffset % 256;    /* lo byte */
  156.  
  157.     /* estimate of bit image size after rotation */
  158.      /* landscape bit_image size = width[bytes] * height[dot-rows] */
  159.          n = w_bytes * h_dots + 16;     /* bytes for descriptor and bit image */
  160.         h = stci_d(res,n);        /* convert to decimal string */
  161.         a = 1; if(n>9) a = 2; if(n>99) a = 3; if(a>999) a = 4;
  162.          
  163.     /* write char size to landscape file, ascii format */
  164.         for(i=0; i<a; i++) {    /* truncate escape byte with 'i<a' */
  165.             h = fputc(res[i],fl);
  166.         }
  167.  
  168.     /* write descriptor string to landscape file */
  169.         for(i=0; i<=16; i++) {
  170.             h = fputc(descriptor[i][1],fl);
  171.         }
  172.         
  173.     /* read image, rotate, and write bitmap to landscape file */
  174.         bitmap(w_bytes, h_bytes, h_dots, h_dots_p, w_bytes_p);
  175.  
  176.         if(size>count) {fcloseall(); return 9;}
  177.  
  178.         rdchar();    /* read 'esc' from next char */
  179.         
  180.         if(filend == 0 && c != '\033') {fcloseall(); return 8;}
  181.         
  182.     } /* 'while' loop */
  183.     
  184.         printf("Font %s created\n",l);
  185.         c = fcloseall();    /* if filend = 0, success */
  186.         return 0;
  187.         
  188. } /* end of convert() */
  189.  
  190.  
  191. /* read bit image of font character and store in map[height(L)][width(L)][1] array */
  192. char bitmap(w_bytes, h_bytes, h_dots, h_dots_p, w_bytes_p)
  193.     unsigned w_bytes;
  194.     unsigned h_bytes;
  195.     unsigned h_dots;
  196.     unsigned h_dots_p;
  197.     unsigned w_bytes_p;
  198. {
  199.     extern unsigned char c;
  200.     extern FILE *fp,*fl;
  201.     unsigned int a,b;
  202.     unsigned char ZERO = '\000';
  203.     int i,j,k,m,p,q;
  204.     
  205.     /* clear only necessary byte pad work space to zero 
  206.         in preparation for reading character data.
  207.         This is along bottom of portrait character */
  208.     for(i=h_dots_p; i<=h_dots_p+8; i++) {    
  209.         for(j=0; j<=w_bytes_p +1; j++) {
  210.             map[j][i][1] = ZERO;
  211.         }
  212.     }
  213.     /* read portrait format pixel data into workspace */
  214.     for(i=1; i<=h_dots_p; i++) {    /* rows in font image */
  215.         for(j=1; j<=w_bytes_p; j++) {
  216.             _read();
  217.             map[j][i][1] = c;    /* map[w_field(P)][dot-row height(P)] */
  218.         } /* column fields */
  219.     } /* rows down image starting with index of one 1*/
  220.  
  221.     /* write rotated pixel data to landscape file 
  222.        This routine forms bytes of rotated image along the
  223.        landscape image width axis. The excess byte padding
  224.        in the origonal portrait image is stripped off; i.e. 
  225.        the pixel data is taken from h_dots, not h_bytes.
  226.        h_dots is equal to portrait image width in dots. */
  227.  
  228.         for(p=h_dots; p>=1; p--) {    
  229.            i = p / 8; /* points to byte, contingent to mod below */
  230.            q = p % 8; /* points to bit as computed in switch below */
  231.            switch(q) {
  232.             case 0:        /* points to next lower byte, bit = LSB */
  233.                 k = 1;    /* k = value of bit position of  */
  234.                 break;    /*   byte used to mask correct bit */
  235.             case 1:        /* points to next higher byte, msb */
  236.                 i += 1;
  237.                 k = 128;  /* first bit is MSB in HP format */
  238.                 break;
  239.             case 2:        
  240.                 i += 1;
  241.                 k = 64;
  242.                 break;
  243.             case 3:
  244.                 i += 1;
  245.                 k = 32;
  246.                 break;
  247.             case 4:
  248.                 i += 1;
  249.                 k = 16;
  250.                 break;
  251.             case 5:
  252.                 i += 1;
  253.                 k = 8;
  254.                 break;
  255.             case 6:
  256.                 i += 1;
  257.                 k = 4;
  258.                 break;
  259.             case 7:
  260.                 i += 1;
  261.                 k = 2;
  262.                 break;
  263.             }        /* there is no possible default */
  264.               for(j=0; j<w_bytes; j++) {
  265.                  c = 0;        /* c is used to form byte for landscape */
  266.              for(m=0,a=128; m<=7; m++,a/=2) { 
  267.                 b = (unsigned int) map[i][j*8+m][1]; 
  268.                 c += a*((k & b)>0);    /* calculate byte value */
  269.                         /* extract bit from portrait and 
  270.                        assign value 'a' for bit */
  271.              } /* bit of pixel data along old port height */
  272.              n = fputc(c,fl);     /* put computed byte 
  273.                             into landscape file */
  274.               } /* j = byte along width (old portrait height) */
  275.             /* k = bit pixel data value used in bitwise mask */
  276.         } /* i = byte along height (old portrait width) from max */
  277.     return NULL;
  278. }
  279.  
  280. unsigned char rdchar () {    /* read byte store in char 'c' */
  281.     extern unsigned char c;
  282.     extern FILE *fp, *fl; 
  283.     int r;
  284.     filepos++;
  285.     c = fgetc(fp);
  286.     if ((filend = feof(fp)) != 0)     return 0; 
  287.     r = fputc(c,fl);
  288.      return   c;
  289. }
  290.  
  291. unsigned char _read() {    /* read byte store in char 'c' */
  292.     extern unsigned char c;
  293.     extern FILE *fp; 
  294.     filepos++;
  295.     c = fgetc(fp);
  296.     if ((filend = feof(fp)) != 0)     return 0; 
  297.      return   c;
  298.  
  299. }
  300.  
  301. unsigned char skip(int n)      {    /* skip 'n' bytes in file */
  302.     extern unsigned char c;
  303.     extern FILE *fp, *fl; 
  304.     int x, r;
  305.     for (x = 0; x < n; ++x)
  306.         { 
  307.         filepos++;
  308.         c = fgetc(fp);
  309.         if ((filend = feof(fp)) != 0) return 0; 
  310.         r = fputc(c,fl);
  311.     }
  312.     return c;
  313.        
  314. unsigned char change(int prev, int new) /* change byte  */
  315.        {
  316.     extern unsigned char c;
  317.     extern FILE *fp, *fl; 
  318.     int r;
  319.     filepos++;
  320.     c = fgetc(fp);
  321.     if (c == prev) r = fputc(new,fl);
  322.     else  r = fputc(new,fl);
  323.     return c;
  324.  
  325. unsigned int measureto (char test)    /* calculate ascii format number (base 10) */
  326.        {
  327.     unsigned char rdchar();
  328.     extern unsigned char c;
  329.     rdchar();
  330.     n = (c - '0');        /* convert from ASCII to integer */
  331.     rdchar();            /* good for 1 to 5 character number */
  332.     if (c == test) return n;
  333.     n = n*10 + (c - '0');
  334.     rdchar();
  335.     if (c == test) return n;
  336.     n = n*10 + (c - '0');
  337.     rdchar();
  338.     if (c == test) return n;
  339.     n = n*10 + (c - '0');
  340.     rdchar();
  341.     if (c == test) return n;
  342.     n = n*10 + (c - '0');
  343.     rdchar();            /* this will read 'test' character */
  344.     return n;            /* with 5 character ascii number   */
  345. }
  346.  
  347. unsigned int _measureto (char test)    /* calculate ascii format number (base 10) */
  348.        {
  349.     unsigned char _read();
  350.     extern unsigned char c;
  351.     _read();
  352.     n = (c - '0');        /* convert from ASCII to integer */
  353.     _read();        /* good for 1 to 5 character number */
  354.     if (c == test) return n;
  355.     n = n*10 + (c - '0');
  356.     _read();
  357.     if (c == test) return n;
  358.     n = n*10 + (c - '0');
  359.     _read();
  360.     if (c == test) return n;
  361.     n = n*10 + (c - '0');
  362.     _read();
  363.     if (c == test) return n;
  364.     n = n*10 + (c - '0');
  365.     _read();            /* this will read 'test' character */
  366.     return n;            /* with 4 character ascii number   */
  367. }
  368.  
  369.